package com.lc.zpyh.ui.activity.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.base.BaseDialog;
import com.lc.umeng.Platform;
import com.lc.umeng.UmengClient;
import com.lc.umeng.UmengLogin;
import com.lc.widget.layout.SettingBar;
import com.lc.zpyh.R;
import com.lc.zpyh.app.AppActivity;
import com.lc.zpyh.http.request.AndroidWeChatLoginApi;
import com.lc.zpyh.http.request.PublicMsgBean;
import com.lc.zpyh.http.request.QQUnbindApi;
import com.lc.zpyh.http.request.QQbindApi;
import com.lc.zpyh.http.request.WXLoginBean;
import com.lc.zpyh.http.request.WechatUnbindApi;
import com.lc.zpyh.other.IntentKey;
import com.lc.zpyh.ui.dialog.MessageDialog;
import com.lc.zpyh.util.SPUtil;
import com.lc.zpyh.widget.UIHelper;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AccountSafetActivity extends AppActivity implements UmengLogin.OnLoginListener{
    @BindView(R.id.sb_binding_phone)
    SettingBar sbBindingPhone;
    @BindView(R.id.sb_binding_vx)
    SettingBar sbBindingVx;
    @BindView(R.id.sb_binding_qq)
    SettingBar sbBindingQq;
    Platform platform;
    @Override
    protected int getLayoutId() {
        return R.layout.activit_accountsafety;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        if (SPUtil.get(this, IntentKey.NEEDTOBINDMOBILEPHONENUMBER,"").equals("1")) {
            sbBindingVx.setRightText("已绑定");
        }else {
            sbBindingVx.setRightText("未绑定");
        }
        if (SPUtil.get(this, IntentKey.USERQQPHONE,"").isEmpty()||SPUtil.get(this, IntentKey.USERQQPHONE,"")==null||SPUtil.get(this, IntentKey.USERQQPHONE,"").equals("")) {
            sbBindingQq.setRightText("未绑定");
        }else {
            sbBindingQq.setRightText("已绑定");

        }

    }

    @OnClick({R.id.sb_binding_phone, R.id.sb_binding_vx, R.id.sb_binding_qq,R.id.sb_password})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.sb_binding_phone:
                UIHelper.startActivity(this,ChangePhoneActivity.class);
                break;
            case R.id.sb_password:
               startActivity(ForgetPassWordActivity.class);
                break;
            case R.id.sb_binding_vx:
                if (SPUtil.get(this, IntentKey.NEEDTOBINDMOBILEPHONENUMBER,"").equals("1")){
//                 解除绑定
                    // 消息对话框
                    new MessageDialog.Builder(getActivity())
                            // 标题可以不用填写
                            .setTitle("解除绑定")
                            // 内容必须要填写
                            .setMessage("确定解除微信绑定？")
                            // 确定按钮文本
                            .setConfirm(getString(R.string.common_confirm))
                            // 设置 null 表示不显示取消按钮
                            .setCancel(getString(R.string.common_cancel))
                            // 设置点击按钮后不关闭对话框
                            //.setAutoDismiss(false)
                            .setListener(new MessageDialog.OnListener() {

                                @Override
                                public void onConfirm(BaseDialog dialog) {
                                    wechatUnbind();
                                }

                                @Override
                                public void onCancel(BaseDialog dialog) {

                                }
                            })
                            .show();

                }else{
                    platform = Platform.WECHAT;
//                toast("也别忘了改微信 " + WXEntryActivity.class.getSimpleName() + " 类所在的包名哦");
                    UmengClient.login(this, platform, this);
                }
                break;
            case R.id.sb_binding_qq:
                if (SPUtil.get(this, IntentKey.USERQQPHONE,"").isEmpty()||SPUtil.get(this, IntentKey.USERQQPHONE,"")==null||SPUtil.get(this, IntentKey.USERQQPHONE,"").equals("")) {
                    platform = Platform.QQ;
                    UmengClient.login(this, platform, this);

                }else {
                    new MessageDialog.Builder(getActivity())
                            // 标题可以不用填写
                            .setTitle("解除绑定")
                            // 内容必须要填写
                            .setMessage("确定解除QQ绑定？")
                            // 确定按钮文本
                            .setConfirm(getString(R.string.common_confirm))
                            // 设置 null 表示不显示取消按钮
                            .setCancel(getString(R.string.common_cancel))
                            // 设置点击按钮后不关闭对话框
                            //.setAutoDismiss(false)
                            .setListener(new MessageDialog.OnListener() {

                                @Override
                                public void onConfirm(BaseDialog dialog) {
//                                    解绑
                                    QQUnbind();
                                }

                                @Override
                                public void onCancel(BaseDialog dialog) {

                                }
                            })
                            .show();

                }

                break;
        }
    }

    private void QQUnbind() {
        EasyHttp.post(this)
                .api(new QQUnbindApi()
                        .setUserId(SPUtil.get(this,IntentKey.USERID,""))).request(new HttpCallback<PublicMsgBean>(this) {


            @Override
            public void onSucceed(PublicMsgBean data) {
                if (data.getCode() == 0) {
                    SPUtil.put(AccountSafetActivity.this,IntentKey.USERQQPHONE,"");
                    sbBindingQq.setRightText("未绑定");
                }

            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }

    private void QQbind(String id) {
        EasyHttp.post(this)
                .api(new QQbindApi()
                        .setUserId(SPUtil.get(this,IntentKey.USERID,"")).setQqOpenId(id)).request(new HttpCallback<PublicMsgBean>(this) {


            @Override
            public void onSucceed(PublicMsgBean data) {
                if (data.getCode() == 0) {
                    toast(data.getMsg());
//                    TODO
                    SPUtil.put(AccountSafetActivity.this,IntentKey.USERQQPHONE,"123");

                    sbBindingQq.setRightText("已绑定");
                }

            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }

    private void wechatUnbind() {
        EasyHttp.post(this)
                .api(new WechatUnbindApi()
                        .setUserId(SPUtil.get(this,IntentKey.USERID,""))).request(new HttpCallback<PublicMsgBean>(this) {


            @Override
            public void onSucceed(PublicMsgBean data) {
                if (data.getCode() == 0) {


                        SPUtil.put(AccountSafetActivity.this,IntentKey.NEEDTOBINDMOBILEPHONENUMBER,"0");


                    sbBindingVx.setRightText("未绑定");
                }

            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }
    private void AndroidWeChatLogin(String token, String id) {
        EasyHttp.post(this)
                .api(new AndroidWeChatLoginApi()
                        .setAccess_token(token).setOpenid(id).setUserId(SPUtil.get(AccountSafetActivity.this, IntentKey.USERID, ""))).request(new HttpCallback<WXLoginBean>(this) {


            @Override
            public void onSucceed(WXLoginBean data) {
//                finish();
                SPUtil.put(AccountSafetActivity.this, IntentKey.PHONE, data.getList().getUserInfo().getUserPhone());
                SPUtil.put(AccountSafetActivity.this, IntentKey.ACCOUNT, data.getList().getUserInfo().getUserAccount());
//                SPUtil.put(LoginCodeActivity.this, IntentKey.TOKEN, data.getList().getUserInfo());
                SPUtil.put(AccountSafetActivity.this, IntentKey.INVITECODE, data.getList().getUserInfo().getInviteCode());
                SPUtil.put(AccountSafetActivity.this, IntentKey.USERID, data.getList().getUserInfo().getUserId() + "");

                if (data.getList().isNeedToBindMobilePhoneNumber()){
                    SPUtil.put(AccountSafetActivity.this, IntentKey.NEEDTOBINDMOBILEPHONENUMBER,"0");
                }else {
                    SPUtil.put(AccountSafetActivity.this, IntentKey.NEEDTOBINDMOBILEPHONENUMBER,"1");

                }
                sbBindingVx.setRightText("已绑定");

            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 友盟登录回调
        UmengClient.onActivityResult(this, requestCode, resultCode, data);
    }
    @Override
    public void onSucceed(Platform platform, UmengLogin.LoginData data) {
        if (isFinishing() || isDestroyed()) {
            // Glide：You cannot start a load for a destroyed activity
            return;
        }

        // 判断第三方登录的平台
        switch (platform) {
            case QQ:
                QQbind(data.getId());

                break;
            case WECHAT:
                AndroidWeChatLogin(data.getToken(), data.getId());
                break;
            default:
                break;
        }



    }

    @Override
    public void onError(Platform platform, Throwable t) {

    }

    @Override
    public void onCancel(Platform platform) {

    }
}
